<?php
class jobAction extends runAction {
	
	public function index() {
		$word = $_REQUEST ['word'];
		foreach ( $word as $k => $v ) {
			$this->assign ( $k, $v );
		}
		$this->assign ( 'tag_title', $_REQUEST ['tag_title'] );
		
		$o = M ( MODULE_NAME );
		
		$where = "1";
		
		$fw = $_REQUEST ['fw'];
		foreach ( $fw as $k => $v ) {
			if ($v) {
				if (! is_utf8 ( $v )) {
					$s1 = iconv ( 'GBK', 'UTF-8', $v );
				}
				$where .= " AND $k = '$v'";
			}
			$this->assign ( $k, $v );
		}
		
		$kw = $_REQUEST ['kw'];
		if ($kw && $kw != '请输入关键字') {
			$where .= " AND title LIKE '%$kw%' or content LIKE '%$kw%'";
		}
		$this->assign ( 'kw', $kw );
		
		$o->where ( $where );
		
		$total = $o->count ();
		$this->assign ( 'total', $total );
		
		$o->field ( 'id,hit,pri,display,title,posttime' );
		
		$o->where ( $where );
		
		$order = $_REQUEST ['order'];
		if ($order == '') {
			$order = 'pri';
		}
		$this->assign ( 'order', $order );
		
		$by = $_REQUEST ['by'];
		if ($by == '') {
			$by = 'DESC';
		}
		$o->order ( "$order $by, id DESC" );
		$this->assign ( 'by', $by );
		
		$ps = $_REQUEST ['ps'];
		if ($ps == '') {
			$ps = '10';
		}
		$o->limit ( $ps );
		$this->assign ( 'ps', $ps );
		$this->assign ( 'ptotal', ceil ( $total / $ps ) );
		
		$p = $_REQUEST ['p'];
		if ($p == '') {
			$p = '1';
		}
		$o->Page ( $p );
		$this->assign ( 'p', $p );
		
		$list = $o->select ();
		$this->assign ( 'list', $list );
		//dump($o);
		$this->display ( MODULE_NAME . ':index' );
	}
	
	public function del() {
		
		if (count ( $_POST ['id'] ) == 0) {
			$this->assign ( 'act_word', '没有选中任何记录' );
			$this->index ();
			exit ();
		}
		
		
		
		import ( 'app.ORG.file' );
		
		// 生成 1,2,3
		$in = '';
		$num = 0;
		foreach ( $_POST ['id'] as $k => $v ) {
			
			if ($v == '') {
				continue;
			}
			
			if ($k != 0) {
				$in .= ',';
			}
			
			$in .= $v;
			$num ++;
			
			// 删除附件
			file::del_dir ( 'upload/' . MODULE_NAME . '/' . $v );
		}
		
		$o = D ( MODULE_NAME );
		$o->where ( "id IN ($in)" )->delete ();
		
		// 回收ID
		$o = M ( MODULE_NAME );
		$o->query ( 'ALTER TABLE __TABLE__ AUTO_INCREMENT = 1' );
		
		$this->assign ( 'act_word', '操作成功 : 已删除 ' . $num . ' 条记录' );
		$this->index ();
	}
	
	public function clean() {
		
		import ( 'app.ORG.file' );
		
		// 删除附件
		file::del_dir ( 'upload/' . MODULE_NAME . '/', 0 );
		
		$o = M ( MODULE_NAME );
		$o->query ( "TRUNCATE TABLE __TABLE__" );
		
		$this->assign ( 'act_word', '清空成功' );
		$this->index ();
	}
	
	public function pri() {
		
		$num = 0;
		$o = M ( MODULE_NAME );
		foreach ( $_POST ['ids'] as $k => $v ) {
			
			if ($_POST ['pri'] [$k] != $_POST ['opri'] [$k]) {
				
				$d ['pri'] = $_POST ['pri'] [$k];
				$o->where ( 'id = ' . $v )->save ( $d );
				$num ++;
			}
		}
		
		if ($num > 0) {
			$this->assign ( 'act_word', '操作成功 : 已更新 ' . mysql_affected_rows () . ' 条记录' );
			$this->index ();
		} else {
			$this->assign ( 'act_word', '没有记录被更新' );
			$this->index ();
		}
	}
	
	public function onoff() {
		
		if (count ( $_POST ['id'] ) == 0) {
			$this->assign ( 'act_word', '没有选中任何记录' );
			$this->index ();
			exit ();
		}
		if ($_POST ['value'] == '') {
			$this->assign ( 'act_word', '未指定开关' );
			$this->index ();
			exit ();
		}
		
		list ( $zd, $onoff ) = explode ( ',', $_POST ['value'] );
		
		$in = '';
		foreach ( $_POST ['id'] as $k => $v ) {
			if ($v != '') {
				if ($k != 0) {
					$in .= ',';
				}
				$in .= $v;
			}
		}
		
		$row [$zd] = $onoff;
		$o = M ( MODULE_NAME );
		$o->where ( "id IN ($in)" )->save ( $row );
		
		$this->assign ( 'act_word', '操作成功 : 已更新 ' . mysql_affected_rows () . ' 条记录' );
		$this->index ();
		
		// $this->success ( '已更新 ' . mysql_affected_rows () . ' 条记录' );
	}
	
	public function view() {
		
		CK ( 'uploaddir', MODULE_NAME );
		
		if (empty ( $_GET ['id'] )) {
			
			// add
			$v = array ('hit' => 0, 'pri' => 0, 'display' => 1, 'date' => date ( 'Y-m-d' ), 'time' => date ( 'H:i:s' ) );
		} else {
			
			// edit
			$o = M ( MODULE_NAME );
			$v = $o->getById ( $_GET ['id'] );
			$v ['date'] = date ( 'Y-m-d', $v ['posttime'] );
			$v ['time'] = date ( 'H:i:s', $v ['posttime'] );
		}
		
		$this->assign ( 'v', $v );
		$this->assign ( 'fid', $_GET ['fid'] );
		
		$this->display ();
	}
	
	public function save() {
		
		import ( 'app.ORG.ztime' );
		
		if ($_POST ['id'] == '') {
			
			// add
			$o = M ( MODULE_NAME );
			if ($o->create ()) {
				$o->display = $_POST ['display'] == 1 ? 1 : 0;
				$o->is_index = $_POST ['is_index'] == 1 ? 1 : 0;
				$o->posttime = ztime::datetime2line ( $_POST ['date'] . ' ' . $_POST ['time'] );
				
				$st = $o->add ();
				
				if ($st !== false) {
					
					$GLOBALS ['id'] = mysql_insert_id ();
					mk_dir ( 'upload/' . MODULE_NAME . '/' . $GLOBALS ['id'] );
					
					foreach ( $_FILES as $key => $val ) {
						if ($val ['tmp_name']) {
							$v = explode ( '.', $val ['name'] );
							$_FILES [$key] ['name'] = $key . '.' . strtolower($v [count ( $v ) - 1]);
						}
					}
					
					$upload = A (upload);
					$upload -> upload();
					
					//$this->assign ( 'way', 'close' );
					$this->success ( '添加成功' );
				} else {
					$this->error ( '添加失败' );
				}
			} else {
				echo $o->getError ();
			}
		
		} else {
			
			// edit
			$o = M ( MODULE_NAME );
			if ($o->create ()) {
				$o->display = $_POST ['display'] == 1 ? 1 : 0;
				$o->is_index = $_POST ['is_index'] == 1 ? 1 : 0;
				$o->posttime = ztime::datetime2line ( $_POST ['date'] . ' ' . $_POST ['time'] );
				
				$st = $o->save ();
				
				if ($st !== false) {
					
					$GLOBALS ['id'] = $_POST ['id'];
					
					foreach ( $_FILES as $key => $val ) {
						 
						if ($val ['tmp_name']) {
							$v = explode ( '.', $val ['name'] );
							$_FILES [$key] ['name'] = $key . '.' . strtolower($v [count ( $v ) - 1]);
						}
					}
					
					$upload = A (upload);
					$upload -> upload();
					//$this->assign ( 'jumbUrl', 'close' );
					$this->success ( '修改成功' );
				} else {
					$this->error ( '修改失败' );
				}
			} else {
				echo $o->getError ();
			}
		
		}
	}
}
?>